const axios = require("axios").default;
const cheerio = require("cheerio");

const { Sequelize } = require("sequelize");

const sequelize = new Sequelize("duyi", "root", "123456", {
  host: "192.168.71.226",
  dialect: "mysql",
  logging: null,
});
const { DataTypes } = require("sequelize");
(function () {
  // 添加一个book表
  return;
  const Book = sequelize.define(
    "Book",
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      imgurl: {
        type: DataTypes.STRING,
      },
      publishDate: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      author: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      paranoid: true,
    }
  );

  sequelize.sync({ alter: true }).then(() => {
    console.log("所有模型同步完成");
  });
})();

// 抓取网站
(function () {
  // return
  /**
   * 获取豆瓣读书网页的源代码
   */
  async function getBooksHTML() {
    const resp = await axios.get("https://book.douban.com/latest");
    // console.log(resp.data);
    return resp.data;
  }
  /**
   * 根据地址得到详情页信息
   */
  async function getBookDetail(url) {
    const resp = await axios.get(url);
    const $ = cheerio.load(resp.data);
    const name = $("h1").text().trim();
    const imgurl = $("#mainpic .nbg img").attr("src");
    const spans = $("#info span.pl");
    const authorSpan = spans.filter((i, ele) => {
      return $(ele).text().includes("作者");
    });
    const author = authorSpan.next("a").text();
    const publishSpan = spans.filter((i, ele) => {
      return $(ele).text().includes("出版年");
    });
    const publishDate = publishSpan[0].nextSibling.nodeValue.trim();
    // console.log({
    //   name,
    //   imgurl,
    //   publishDate,
    //   author,
    // });
    return {
      name,
      imgurl,
      publishDate,
      author,
    };
  }
  /**
   * 获取所有的书籍信息
   */
  async function fetchAll() {
    const links = await getBookLinks();
    const list = links.map(async (link) => {
      return await getBookDetail(link);
    });
    Promise.all(list).then((res) => {
      console.log(res);
      // 这里就拿到数据了
      // 导入数据库就可以了
    });
  }
  /**
   * 从豆瓣读书得到网页,分析得到书籍的数组
   */
  async function getBookLinks() {
    const html = await getBooksHTML();
    // console.log(html);
    const $ = cheerio.load(html);
    const achorElements = $("#content .chart-dashed-list li a.fleft");
    const links = achorElements
      .map((index, ele) => {
        return ele.attribs["href"];
      })
      .get();
    return links;
  }
  //   getBookLinks();
  //   getBookDetail("https://book.douban.com/subject/36791197/");
  fetchAll();
})();
